package br.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import br.com.bean.EnsinoBean;
import br.com.bean.SerieBean;


public class SerieDao {

	public void inserir (Connection conn, SerieBean value) throws Exception{
		
		String sql = "INSERT INTO serie (id_ensino, serie) VALUES (?, ?)";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getEnsino().getId());
		ps.setString(2, value.getDesc());
		ps.executeUpdate();
		ps.close();
		
	}
	
	public void alterar (Connection conn, SerieBean value) throws Exception{
		
		String sql = "UPDATE serie SET id_ensino=?, serie=? WHERE  id = ?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getEnsino().getId());
		ps.setString(2, value.getDesc());
		ps.execute();
		ps.close();

	}
	
	public void excluir (Connection conn, SerieBean value) throws Exception {

		String sql = "DELETE FROM serie WHERE where id = ?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ps.executeUpdate();
	}
	
	public ArrayList<SerieBean> getLista(Connection conn, SerieBean value) throws Exception{

		String sql = "SELECT * FROM cad_serie where id = ?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getEnsino().getId());
		ResultSet rs = ps.executeQuery();
		ArrayList<SerieBean> listaSerie = new ArrayList<SerieBean>();
		while(rs.next()){
			SerieBean serie = new SerieBean();
			serie.setId(rs.getInt("id"));
			serie.getEnsino().setId(rs.getInt("id_ensino"));
			serie.setDesc(rs.getString("serie"));
			listaSerie.add(serie);
		}
		return listaSerie;
	}
	
	public ArrayList<SerieBean> getLista(Connection conn) throws Exception{

		String sql = "SELECT id_ensino, serie FROM serie";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		ArrayList<SerieBean> listaSerie = new ArrayList<SerieBean>();
		while(rs.next()){
			SerieBean serie = new SerieBean();
			serie.getEnsino().setId(rs.getInt("id_ensino"));
			serie.setDesc(rs.getString("serie"));
			listaSerie.add(serie);
		}
		return listaSerie;
	}
	// combo dinamico de serie ensino
	public List<SerieBean> getListaSerie (Connection conn, EnsinoBean value) throws Exception{

		String sql = "SELECT * FROM serie where id_ensino = ?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ResultSet rs = ps.executeQuery();
		List<SerieBean> listaSerie = new ArrayList<SerieBean>();
		SerieBean serie = null;
		while(rs.next()){
			serie = new SerieBean();
			serie.setId(rs.getInt("id"));
			serie.setDesc(rs.getString("serie"));
			listaSerie.add(serie);
		}
		return listaSerie;
	}
	
}
